package main

import "fmt"

func search(nums []int, target int) int {
	l, r := 0, len(nums)-1
	if r == -1 {
		return -1
	}
	for l <= r {
		m := (r-l)/2 + l
		if nums[m] == target {
			return m
		} else if nums[m] > target {
			r = m - 1
		} else if nums[m] < target {
			l = m + 1
		}
	}
	return -1

}

func main() {
	fmt.Println("开始")
	fmt.Println("4:  ", search([]int{1, 2, 3, 4, 5}, 5))
	fmt.Println("4:  ", search([]int{-1, 0, 3, 5, 9, 12}, 9))
	fmt.Println("-1:  ", search([]int{5}, 5))
	fmt.Println("-1:  ", search([]int{-1, 0, 3, 5, 9, 12}, 2))
}
